<!-- A page that is used to test that a dynamic form fill feature works properly when select options are added/removed. -->
<body>
  <form name="addr1.1" id="form1" action="https://example.com/" method="post">
    Name: <input type="text" name="firstname" id="firstname" autocomplete="given-name"><br>
    Address: <input type="text" name="address1" id="address1"><br>
    City: <input type="text" name="city" id="city"><br>
    State: <select name="state" id="state">
      <option value="ON">ON</option>
      <option value="QC">QC</option>
      <option value="AB">AB</option>
    </select> <br>
    Zip: <input name="zip" id="zip"> <br>
    Country: <select name="country" id="country" onchange="CountryChanged()">
      <option value="CA">Canada</option>
      <option value="US">United States</option>
    </select> <br>
    Company: <input name="company" id="company"> <br>
    Email: <input name="email" id="email"> <br>
    Phone: <input name="phone" id="phone"> <br>
    <input type="reset" value="Reset">
    <input type="submit" value="Submit" id="profile_submit">
  </form>
</body>

<script>

var notify_on_first_name_input_change = false;
var first_name_input_changed = false;

function CountryChanged() {
  var first_name_input = document.getElementById("firstname");
  // Reset the value of the first name field.
  first_name_input.value = '';
  first_name_input.addEventListener('change', function() {
    first_name_input.value = '';
    first_name_input_changed = true;

    setTimeout(function() {
      // Re-change the select options.
      for (var i=0; i<state_select.length; i++) {
         state_select.remove(i);
      }
      state_select.options[0] = new Option('CA', 'CA');
      state_select.options[1] = new Option('MA', 'MA');
      state_select.options[2] = new Option('TX', 'TX');
    });

    if (notify_on_first_name_input_change) {
      // Give time to see if it fills.
      setTimeout(function() {
        window.domAutomationController.send(first_name_input.value != '');
      }, 1000);
    }
  });

  // Reset the value of the address field.
  document.getElementById("address1").value = '';

  // Get the select object.
  var state_select = document.getElementById("state");

  // Remove the provinces options.
  for (var i=0; i<state_select.length; i++) {
     state_select.remove(i);
  }

  // Add the states options.
  state_select.options[0] = new Option('WA', 'WA');
  state_select.options[1] = new Option('MA', 'MA');
  state_select.options[2] = new Option('TX', 'TX');
}

function hasRefilled() {
  var first_name_input = document.getElementById('firstname');
  if (first_name_input && first_name_input_changed) {
    // Give time to see if it fills.
    setTimeout(function() {
      window.domAutomationController.send(document.getElementById("firstname").value != '');
    }, 1000);
  } else {
    notify_on_first_name_input_change = true;
  }
}

</script>
